var rowIndex = "%^%%%%";
var regid = 0;
var flag = true;
var page;
var _fBillNo;
var _fDetailID;
var _fDate;
var _fSupplyName,_fTaxrate;
var gg;
var orderNumber;
var _index;
var _clickIndex; // 获取到货模态框里选中行下标

//数组方法
function isAllEqual(array) {
    if (array.length > 0) {
        return !array.some(function (value, index) {
            return value !== array[0];
        });
    } else {
        return true;
    }
}
/*
 * @Description: 新增按钮点击事件
 * @param :
 */
$("#addCheck").click(function() {
	var daoData = [];
	var _getData = $('#inTable').bootstrapTable('getSelections');
	$.each(_getData,function(key,value){
		daoData = {
				"fEntryID":value.fEntryID,
				"fNumber":value.fNumber,
				"fName":value.fName,
				"fModel":value.fModel,
				"fQty":value.fQty,
				"fArrivalDate":value.fArrivalDate,
				"fCommitQty":value.fCommitQty,
				"fAuxCommitQty":value.fAuxCommitQty,
				"fStockID":value.fStockID,
				"fSourceInterId":value.fSourceInterId,
				"fSourceEntryID":value.fSourceEntryID,
				"fClosed":value.fClosed,
				"fDetailID":value.fDetailID,
				"fSecCoefficient":value.fSecCoefficient,
				"fTaxrate":value.fTaxrate,
				"fTaxPrice":value.fTaxPrice,
				"fPrice":value.fPrice,
				"fUnitName":value.fUnitName,
				"fTemp1Qty":value.fTemp1Qty,
				"fTempQty":value.fTempQty,
				"fHelpCode":value.fHelpCode
		}
	});
	console.log(daoData.fArrivalDate);
	$('#daoTable').bootstrapTable('append',{"fEntryIDs":"","fArrivalDate":dateFormatter(daoData.fArrivalDate),"fUnitName":daoData.fUnitName,"fCommitQty":0,"fStockID":daoData.fStockID,"fClosed":0});
	//$('#daoTable').bootstrapTable('updateRow',{index:_index,row:{"fNumber":"11"}});
});
/*
 * @Description: 到货按钮点击事件 
 * @param :
 */
$("#arrive").click(function(){
	//_fBillNo=row.fBillNo;
	var dataJson=[];
	var daoData = [];
	var daojson = [];
	var fSupplyName;
	var _getData = $('#inTable').bootstrapTable('getSelections');
	if(_getData==''){
		swal_Tips("请勾选");
		return false;
	}
	$.each(_getData,function(key,value){
		var fSecCoefficient = Math.round(parseFloat(value.fSecCoefficient)*100)/100;
		var fTemp1Qty = 0;
		if(fSecCoefficient != 0){
			fTemp1Qty = Math.round(parseFloat(Math.round(parseFloat(value.fQty)*100)/100-Math.round(parseFloat(value.fCommitQty)*100)/100) / parseFloat(value.fSecCoefficient) *100) / 100;
		}
		daoData = {
				"fEntryID":value.fEntryID,
				"fNumber":value.fNumber,
				"fName":value.fName,
				"fModel":value.fModel,
				"fQty":Math.round(parseFloat(value.fQty)*100)/100,
				"fArrivalDate":value.fArrivalDate,
				"fCommitQty":Math.round(parseFloat(value.fCommitQty)*100)/100,
				"fAuxCommitQty":value.fAuxCommitQty,
				"fStockID":value.fStockID,
				"fSourceInterId":value.fSourceInterId,
				"fSourceEntryID":value.fSourceEntryID,
				"fClosed":value.fClosed,
				"fDetailID":value.fDetailID,
				"fSecCoefficient": fSecCoefficient,
				"fTaxrate":value.fTaxrate,
				"fTaxPrice":Math.round(parseFloat(value.fTaxPrice)*100000000)/100000000,
				"fPrice":Math.round(parseFloat(value.fPrice)*100000000)/100000000,
				"fUnitName":value.fUnitName,
				"fTemp1Qty": fTemp1Qty,
				"fTempQty":Math.round(parseFloat(value.fQty)*100)/100-Math.round(parseFloat(value.fCommitQty)*100)/100,
				"fHelpCode":value.fHelpCode
		}
		//console.log(daoData);
		daojson.push(daoData);
		dataJson.push(value.fSupplyName);
		_fBillNo=value.fBillNo;
		_fDetailID=value.fDetailID;
		_fDate=value.fDate;
		_fSupplyName=value.fSupplyName;
		_fTaxrate=Math.round(parseFloat(value.fTaxrate))/100;
	});
	//console.log(daojson);
	if(!isAllEqual(dataJson)){
		swal_Tips("供应商不同 请重新选择");
		return false;
	}
//	获取id和name
	$.ajax({
		url: pathUrl("user/getSession.action"),
		type: 'POST',
		cache: false,
		dataType: 'JSON',
		success: function(data) {
			console.log(data);
			$('#fWorker').val(data.userCode);
			$('#fWorkerName').val(data.name);
		},
		error: function() {
		}
	});
	$('#daoTable').bootstrapTable('destroy').bootstrapTable({
		//url : pathUrl("poOrder/getPOOrderByBillNo.action"),//请求后台的URL
		//height : $(window).height() - 200,//行高
		//method : "POST",//请求方式
		//striped : true,//是否显示行间隔色
		//cache : false,//是否使用缓存，默认为true
		pagination : false,//是否显示分页
		clickToSelect : true,//是否启用点击选中行
		/*singleSelect : true,//是否单选
*/		pageNumber : 1,//初始化加载第几页
		pageSize : 5,//每页的记录行数
		//sidePagination : "server", // 分页方式：client客户端分页，server服务端分页（*）
		pageList : [ 5, 15, 30, 50, 100 ],// 分页方式：client客户端分页，server服务端分页
		queryParams : function(params) {//传递参数
			var data= {};
			data.offset = params.offset;
			data.row = params.limit;
			//data.sortOrder = params.order;//排序
			//data.sortName = params.sort;//排序
			//data.fBillNo = _fBillNo;
			return data;
		},
		dataType : 'JSON',//数据类型
		uniqueId : "fEntryID",//每一行的唯一标识，一般为主键列
		columns : [{
			field : 'fEntryID',
			align : 'left',
			valign: 'middle',
			sortable : true,
			title : '序</br>号',
			edit:false,
			formatter: function (value, row, index) {
				_index=index
//				console.log(_index);
		        return index+1;
		    }
		},{
			field : "fNumber",
			align : 'left',
			valign: 'middle',
			title : '物料代码',
			edit : true
		}, {
			field : "fName",
			align : 'left',
			valign: 'middle',
			title : '物料名称',
			edit : false
		},{
			field : 'fHelpCode',
			align : 'left',
			valign: 'middle',
			title : '助记码',
			edit:false
		}, {
			field : "fModel",
			align : 'left',
			valign: 'middle',
			title : '规格型号',
			edit : false
		}, {
			field : "fQty",
			align : 'left',
			valign: 'middle',
			title : '采购</br>数量',
			edit : true
		}, {
			field : "fArrivalDate",
			align : 'left',
			valign: 'middle',
			title : '预计</br>到货</br>日期',
			formatter :dateFormatter,
			edit : false
		}, {
			field : "fSecCoefficient",
			align : 'left',
			valign: 'middle',
			title : '换</br>算</br>率',
			edit : false
		}, {
			field : "fUnitName",
			align : 'left',
			valign: 'middle',
			title : '单</br>位',
			edit : false
		}, {
			field : "fCommitQty",
			align : 'left',
			valign: 'middle',
			title : '已</br>到</br>数',
			edit : false
		}, {
			field : "fStockID",
			align : 'left',
			valign: 'middle',
			title : '仓库',
			width : 100,
			edit : {
				type : 'select',// 下拉框
		    	url : pathUrl("stock/getStockList.action"),
				method : 'POST',//请求方式（必填）
		    	valueField : 'fStockID',
		    	textField : 'fStockName',
		    	onSelect : function(val,rec){
//		    		console.log(val,rec)
		    		var $tr = $(this)[0].$el.closest("tr");
		    		var index = $tr.data('index');
		    		var row = {};
		    		row.fStockID = val;
					$('#newProcessTable').bootstrapTable('updateRow',{index: index,row:row});
					/*hideRow();*/
		    	}
			}
		},  {
			field : "fTempQty",
			align : 'left',
			valign: 'middle',
			title : '到</br>货</br>数',
			edit : true
			
		},  {
			field : "fTemp1Qty",
			align : 'left',
			valign: 'middle',
			title : '辅助</br>到货</br>数',
			edit : true,
			/*formatter : function(value,row,index){
				gg=row.fNumber.substring(0,2);
				orderNumbert = index;
				if(value == ""){
					return "";
				}else{
					return parseFloat(value);
				}
			}*/
			
		}, {
			field : "fPrice",
			align : 'left',
			valign: 'middle',
			title : '单</br>价',
			edit : true,
			formatter : function(value,row,index){
				orderNumber = index;
				if(value == ""){
					return "";
				}else{
					return parseFloat(value);
				}
			}
		}, {
			field : "fTaxPrice",
			align : 'left',
			valign: 'middle',
			title : '含税</br>单价',
			edit : true,
		},{
			field : 'fClosed',//如需导出execl field必填operate，否则操作行会被导出
			align : 'left',
			valign: 'middle',
			title : '是否</br>关闭',
			edit:{
				type : 'select',// 下拉框
		    	data : [{id:"1",text:'是'},{id:'0',text:'否'}]
			}
		}, {
			field : "c",
			align : 'left',
			valign: 'middle',
			title : '操作',
			edit : true,
			formatter : function(value,row,index){
				return '<button type="button" class="btn btn-default deleteget" style="font-size:12px;margin-right:5px;">删除</button>';
			},
			events : operateEvents,
			edit:false,
		}],
		onClickRow : function(row, $element) {//行单击
//			console.log(row)
			_clickIndex = $element.closest("tr").data('index');
			console.log(_clickIndex)
		},
		onClickCell :function(field, value, row, $element){
			console.log(field);
			if(field=="fNumber"){
				$("input").unbind().bind("keyup",function (event) {
					if(event.keyCode == "118"){
						IDName = event.target.id;
						$("#queryName").text("查询-物料代码");
						$("#ERPModal").modal('show');
					}
				});
			}
		}
	});
	$('#daoTable').bootstrapTable('load', daojson);
$("#fno").val(_fBillNo);
$("#fDetailID").val(_fDetailID)
$("#infTranOutQty").val(dateFormatter(_fDate));
$("#infSupplierName").val(_fSupplyName);
$("#inputCardModal").modal('show');
});
/*
* @Description: 操作事件
* @param :
*/
window.operateEvents = {
	'click .deleteget' : function(e, value, row, index) {//到货按钮点击事件
		showSwal(
			'删除',
			'您确定删除吗？',
			'是的，确定删除',
			function(result){
				if(result.value){
					console.log(row.fEntryID);
					 $('#daoTable').bootstrapTable('removeByUniqueId', row.fEntryID);
				}
			}
		)
		console.log(row);
	}
};
/*
 * @Description: 复制点击事件 
 * @param :
 */
$("#copys").click(function(){
	let dataJson=[];
	let daoData = [];
	let daojson = [];
	let _getData = $('#inTable').bootstrapTable('getSelections');
	if(_getData==''){
		swal_Tips("请勾选");
		return false;
	}
	$.each(_getData,function(key,value){
		daoData = {
				"fEntryID":value.fEntryID,
				"fNumber":value.fNumber,
				"fName":value.fName,
				"fModel":value.fModel,
				"fQty":value.fQty,
				"fArrivalDate":value.fArrivalDate,
				"fCommitQty":value.fCommitQty,
				"fAuxCommitQty":value.fAuxCommitQty,
				"fStockID":value.fStockID,
				"fSourceInterId":value.fSourceInterId,
				"fSourceEntryID":value.fSourceEntryID,
				"fClosed":value.fClosed,
				"fDetailID":value.fDetailID,
				"fSecCoefficient":value.fSecCoefficient,
				"fTaxrate":value.fTaxrate,
				"fTaxPrice":value.fTaxPrice,
				"fPrice":value.fPrice,
				"fUnitName":value.fUnitName,
				"fTemp1Qty":value.fTemp1Qty,
				"fTempQty":value.fTempQty,
				"fInterID":value.fInterID,
				"fItemID":value.fItemID
		}
		daojson.push(daoData);
	});
	console.log(daojson);
	$('#copyTable').bootstrapTable('destroy').bootstrapTable({
		pagination : false,//是否显示分页
		clickToSelect : true,//是否启用点击选中行
		/*singleSelect : true,//是否单选
*/		pageNumber : 1,//初始化加载第几页
		pageSize : 5,//每页的记录行数
		//sidePagination : "server", // 分页方式：client客户端分页，server服务端分页（*）
		pageList : [ 5, 15, 30, 50, 100 ],// 分页方式：client客户端分页，server服务端分页
		queryParams : function(params) {//传递参数
			var data= {};
			data.offset = params.offset;
			data.row = params.limit;
			return data;
		},
		dataType : 'JSON',//数据类型
		uniqueId : "id",//每一行的唯一标识，一般为主键列
		columns : [{
			field : "fNumber",
			align : 'left',
			valign: 'middle',
			title : '物料代码',
			edit : false
		}, {
			field : "fName",
			align : 'left',
			valign: 'middle',
			title : '物料名称',
			edit : false
		}, {
			field : "fModel",
			align : 'left',
			valign: 'middle',
			title : '规格型号',
			edit : false
		}, {
			field : "fQty",
			align : 'left',
			valign: 'middle',
			title : '采购</br>数量'
		}, {
			field : "fArrivalDate",
			align : 'left',
			valign: 'middle',
			title : '预计</br>到货</br>日期',
			formatter :dateFormatter,
			edit : false
		}, {
			field : "fSecCoefficient",
			align : 'left',
			valign: 'middle',
			title : '换</br>算</br>率',
			edit : false
		}, {
			field : "fUnitName",
			align : 'left',
			valign: 'middle',
			title : '单</br>位',
			edit : false
		}, {
			field : "fCommitQty",
			align : 'left',
			valign: 'middle',
			title : '已</br>到</br>数',
			formatter : function(value,row,index){
				if(value == undefined){
					return '';
				}else{
					return 0;
				}
			},
			edit : false
		}, {
			field : "fStockID",
			align : 'left',
			valign: 'middle',
			title : '仓库',
			width : 100,
			edit : {
				type : 'select',// 下拉框
		    	url : pathUrl("stock/getStockList.action"),
				method : 'POST',//请求方式（必填）
		    	valueField : 'fStockID',
		    	textField : 'fStockName',
		    	onSelect : function(val,rec){
//		    		console.log(val,rec)
		    		var $tr = $(this)[0].$el.closest("tr");
		    		var index = $tr.data('index');
		    		var row = {};
		    		row.fStockID = val;
					$('#newProcessTable').bootstrapTable('updateRow',{index: index,row:row});
					/*hideRow();*/
		    	}
			}
		},  {
			field : "fTempQty",
			align : 'left',
			valign: 'middle',
			title : '到</br>货</br>数',
			edit : false,
			formatter : function(value,row,index){
				if(value == undefined){
					return '';
				}else{
					return 0;
				}
			}
			
		}, {
			field : "fPrice",
			align : 'left',
			valign: 'middle',
			title : '单</br>价',
			edit : false,
			formatter : function(value,row,index){
				if(value == ""){
					return "";
				}else{
					return parseFloat(value);
				}
			}
		}],
		onClickRow : function(row, $element) {//行单击
//			console.log(row)
		},
		onLoadSuccess:function(row, $element) {
			console.log(row);
		}
	});
	$('#copyTable').bootstrapTable('load', daojson);
    $("#copyinputCardModal").modal('show');
});
/*
 * @Description: 修改对应事件
 * @param :
 */

$("#daoTable").on('editable-save.bs.table',function($el,field, row, oldValue){
	var markss=true;
	var value = {};
	row.fTaxPrice = Math.round((parseFloat(row.fPrice)*_fTaxrate+parseFloat(row.fPrice))*100) / 100;
	value.fTaxPrice = row.fTaxPrice;
	$('#daoTable').bootstrapTable('updateRow',{index: _clickIndex,row:value});
	if(row.fSecCoefficient == 0){ // 换算率
		markss=false;
		if(row.fTemp1Qty!=0){
			swal_Tips("换算率为0，辅助到货数不能修改");
			row.fTemp1Qty=0;
			$('#daoTable').bootstrapTable('updateRow',{index: _clickIndex,row:value});
			return false;
		}
		
	}
	if(field == "fQty"){ // 采购数量
		var fTemp1Qty = 0, fQty = row.fQty;
		if(row.fSecCoefficient != 0){
			fTemp1Qty = Math.round(parseFloat(fQty) / parseFloat(row.fSecCoefficient) *100) / 100;
		}
		value.fTempQty = fQty;
		value.fTemp1Qty = fTemp1Qty;
		value.fPrice = row.fPrice;
		$('#daoTable').bootstrapTable('updateRow',{index: _clickIndex,row:value});
	}
	if(field == "fTempQty"){ // 到货数	
		value.fTemp1Qty = Math.round(parseFloat(row.fTempQty) / parseFloat(row.fSecCoefficient) *100) / 100;
		$('#daoTable').bootstrapTable('updateRow',{index: _clickIndex,row:value});
	}
//	if(parseFloat(row.fTempQty)>parseFloat(row.fQty)-parseFloat(row.fCommitQty)){
//		swal_Tips("到货数不能大于采购数量减已到货数");
//		row.fTempQty=0;
//		return false;
//	}
	if(row.fTempQty>0){ 
		markss=false;
	}else{
		row.fTempQty = (parseFloat(row.fTemp1Qty)*parseFloat(row.fSecCoefficient))*10000000;
		value.fTempQty = Math.round(row.fTempQty)/10000000;
	}
	if(markss){
		$('#daoTable').bootstrapTable('updateRow',{index: _clickIndex,row:value});
	}
})

var resetColumn = [ 
	{
		checkbox : true,
		align : 'left',
		valign: 'middle',
		edit: false
	}, {
		field : 'fBillNo',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '采购单号',
		visible : true,
		must:true,
		width: '10%',
		edit:false
	}, {
		field : 'fSupplyName',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '供应商',
		visible : true,		
		width: '10%',
		edit:false
	}, {
		field : 'fEntryID',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '序号',
		visible : true,		
		width: '10%',
		edit:false,
		showSearch:false
	}, {
		field : 'fNumber',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '物料代码',
		visible : true,		
		width: '10%',
		edit:false
	},{
		field : 'fName',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '物料名称',
		visible : true,
		width: '10%',
		edit:false
	},{
		field : 'fHelpCode',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '助记码',
		visible : true,
		width: '10%',
		edit:false
	},{
		field : 'fModel',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '规格型号',
		visible : true,		
		width: '10%',
		edit:false
	}, {
		field : 'fQty',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '采购数',
		visible : true,		
		width: '10%',
		edit:false,
		showSearch:false,
		formatter:function(value,row,index) {
			return Math.round(parseFloat(value)*100)/100;
		}
	}, {
		field : 'fSecQty',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '辅助</br>采购数',
		visible : true,		
		width: '10%',
		edit:false,
		showSearch:false,
		formatter:function(value,row,index) {
			return Math.round(parseFloat(value)*100)/100;
		}
	}, {
		field : 'fCommitQty',
		align : 'left',
		valign: 'left',
		sortable : true,
		title : '到货数',
		visible : true,		
		width: '10%',
		edit:false,
		showSearch:false,
		formatter:function(value,row,index) {
			return Math.round(parseFloat(value)*100)/100;
		}
	}, {
		field : 'fSecCommitQty',
		align : 'left',
		valign: 'left',
		sortable : true,
		title : '辅助</br>到货数',
		visible : true,		
		width: '10%',
		edit:false,
		showSearch:false,
		formatter:function(value,row,index) {
			return Math.round(parseFloat(value)*100)/100;
		}
	}, {
		field : '',
		align : 'left',
		valign: 'left',
		sortable : true,
		title : '关联数量',
		visible : true,		
		width: '10%',
		edit:false,
		showSearch:false,
		/*formatter:function(value,row,index) {
			return Math.round(parseFloat(value)*100)/100;
		}*/
	}, {
		field : '',
		align : 'left',
		valign: 'left',
		sortable : true,
		title : '关联辅助</br>到货',
		visible : true,		
		width: '10%',
		edit:false,
		showSearch:false,
		/*formatter:function(value,row,index) {
			return Math.round(parseFloat(value)*100)/100;
		}*/
	},{
		field : 'fStockQty',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '入库数',
		visible : true,
		width: '10%',
		edit:false,
		showSearch:false,
		formatter:function(value,row,index) {
			return Math.round(parseFloat(value)*100)/100;
		}
	},{
		field : 'fSecStockQty',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '辅助</br>入库数',
		visible : true,
		width: '10%',
		edit:false,
		showSearch:false,
		formatter:function(value,row,index) {
			return Math.round(parseFloat(value)*100)/100;
		}
	}, {
		field : 'fDate',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '下单日期',
		formatter : isNull,
		visible : true,		
		width: '10%',
		formatter:dateFormatter,
		edit:false
	}, {
		field : 'fArrivalDate',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '预计到货日期',
		formatter : isNull,
		visible : true,		
		width: '10%',
		formatter:dateFormatter,
		edit:false
	}/*, {
		field : 'operate',
		align : 'left',
		valign: 'middle',
		title : '操作',
		formatter : isNull,
		visible : true,		
		width: '10%',
		must:true,
		formatter : function(value,row,index){
			if(row.fQcStatus==0||row.fQcStatus==null){
				return '<button type="button" class="btn btn-default in" style="font-size:12px;margin-right:5px;">到货</button>';
			}else{
				return '';
			}
		},
		events : operateEvents,
		edit:false,
		showSearch:false
	}*/];
var dataExport = {/*
	'fDate' : $("#fDate").val(),*/
	'fOperation' : $("#fOperation").val(),
	'fMethod' : $("#fMethod").val(),
	'fIP' : $("#fIP").val(),
	'fLevel' : $("#fLevel").val(),
	'fParams' : $("#fParams").val(),
	'fBillerName' : $("#fBillerName").val(),
	'fExplanation' : $("#fExplanation").val(),
	'row' : 10000000
};

var colArr = [];
/*
 * @Description: 预加载 
 * @param :
 */
$(function (){
	resizeIframeHight();
//	initTable();
	initField();
	loadRecordName("CGDH",initTable);
	$(".CGDH",parent.document).click(function(){
		$('#inTable').bootstrapTable('refresh');
	});
});

/*
 * @Description: 初始化委外转出/接收表
 * @param : 
 */
function initTable(){
	$("#inTable").bootstrapTable('destroy').bootstrapTable({
		url : pathUrl("poOrder/notArrivalPoOrderList.action"),
		height : $(window).height()*0.82,
		method : "POST",
		striped : true,
		cache : false,
		clickToSelect : true,
//		editable:false,//开启编辑模式
		sidePagination : "server", // 分页方式：client客户端分页，server服务端分页（*）
		pagination : true,
		pageNumber : 1,
		pageSize : 30,
		pageList : [ 10, 30, 60, 100, 'ALL' ],
		dataExport : dataExport,
		showExport: true,
		colArr : colArr,
		exportDataType: "allServer",
		buttonsAlign:"right",  //按钮位置
		rowStyle : function(row, index) {
			var classes = [ 'success' ];
			if (index == rowIndex) {
				return {
					classes : classes[0]
				};
			}
			return {};
		},
		queryParams : function(params) {
			var data = {};
			var orderBy = params.sort + " " + params.order;      //排序列名
			if(params.sort==undefined){
				orderBy = "fDate desc";
			}
			data.offset = params.offset +1;
			data.row = params.limit,
			data.sqlStr = sqlCombine;
			data.orderBy = orderBy;
			return data;
		},
		dataType : 'JSON',
		uniqueId : "id",
		columns : customColumn,
		onClickRow: function (row,$element) {
//			console.log(row)
			$('.success').removeClass('success');// 去除之前选中的行的，选中样式
			$($element).addClass('success');// 添加当前选中的 success样式用于区别
			index = $element.data('index');
			rowIndex = index;
			_index = index;
			page = $('.pagination .active').find("a").eq(0).text();
		},
		onLoadSuccess:function(data){
			console.log(data);
			hebing();
		},
		onPageChange : function(number, size) {
			if (page == number) {
				rowIndex = index;
			} else {
				$('.success').removeClass('success');
				rowIndex = "%^%%%%";
			}
		},
		exportHandler : function(text){
			if(text == 'success'){
				$('#export').text('导出成功');
				setTimeout(function(){
					$('#export').text('导出');
					$('#export').blur();
					$('#export').removeAttr('disabled');
				},3000)
			}
		}
	});
}

/*
 * @Description: 重置按钮点击事件 
 * @param :
 */
$(".resetting").click(function(){
	$(".clean_C").val("");
	var data = dataExport;
	for(prop in data){
		if(data.hasOwnProperty(prop)){
			data[prop] = '';
		}
	}
	reset();
});

/*
 * @Description: 重置
 * @param :
 */
function reset(){
	$("#infoTable").bootstrapTable('destroy'); 
	initTable();
	$('.success').removeClass('success');
	rowIndex = "%^%%%%";
}
/*
 * @Description: more按钮点击事件
 * @param : 
 */
$("#more").click(function(){
	if(flag){
		$("#more").text("隐藏");
		$("#extend").show();
		flag = false;
	}else{
		$("#more").text("更多");
		$("#extend").hide();
		flag = true;
	}
	$('#inTable').bootstrapTable('resetWidth');
});

/*
 * @Description: 数据内容更改赋值给dataExport
 * @param :
 */
$('.page-heading .clean_C').on('change',function(){
	var attr = $(this).attr('name');
	dataExport[attr] = $(this).val();
});
/*
 * @Description: 导出按钮点击事件
 * @param :
 */
$('#export').click(function(e){
	$('#inTable').exportColumn();
});

/*
 * @Description: 查询条件按回车事件
 * @param :
 */
$("#fDate,#fOperation,#fMethod,#fIP,#fLevel,#fParams,#fBillerName,#fExplanation").keypress(function(event){
	if(event.keyCode == "13"){
		initTable();
	}
});

/*
 * @Description: 空字符串格式化
 * @param :
 */
function isNull(value,row,index){
	if(value == null){
		return "";
	}else{
		return value;
	}
}

function hideRow(){
	var length = $(".warning").length;
	if(value == 0 && length != 0){
		for(var x = 0;x < length;x++){
			document.getElementsByClassName("warning")[x].style.display = "none";//隐藏fIsMerge=2
		}
	}
}

/*
 * @Description: 员工ID输入框输入事件：输入员工ID后，自动带出员工姓名
 * @param : 
 */
validateID("#fWorker","#fWorkerName","#save");

$("#confirm").click(function() {
	var flag = 1;
	var dataJson=[];
	var datass={};
	var datas = $("#daoTable").bootstrapTable('getData');//获取的row
	//console.log(datas);
	var reg1 = /^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$/;
	var reg2 = /^\d+$/;
	$.each(datas,function(key,value){
		/*if(value.fSourceInterId == null || value.fSourceInterId == ""){
			flag = 0;
			swal_Tips("请填写序号"+value.fEntryID+"所对应物料的到货数量");
			return false;
		}
		  if (!reg1.test(value.fSourceInterId) && !reg2.test(value.fSourceInterId)){
			  flag = 0;
				swal_Tips("序号"+value.fEntryID+"所对应物料的到货数量应为数字");
				return false;
		  }
		if(value.fNumber.substring(0,2) == "02" && value.fSourceEntryID == null){
			flag = 0;
			swal_Tips("请填写序号"+value.fEntryID+"所对应物料的辅助到货数量");
			return false;
		}
		if(value.fNumber.substring(0,2) == "02" && parseFloat(value.fSourceInterId) == 0.0 && parseFloat(value.fSourceEntryID) != 0.0){
			flag = 0;
			swal_Tips("序号"+value.fEntryID+"所对应物料的到货数量为0，辅助到货数量也应为0");
			return false;
		}
		if(value.fNumber.substring(0,2) != "02" && value.fSourceEntryID != null && value.fSourceEntryID != ""){
			flag = 0;
			swal_Tips("不能填写序号"+value.fEntryID+"所对应物料的辅助到货数量");
			return false;
		}

		if(value.fNumber.substring(0,2) == "02" && value.fSourceEntryID != null && value.fSourceEntryID != ""
			&& (!reg1.test(value.fSourceEntryID)) && (!reg2.test(value.fSourceEntryID))){
			flag = 0;
			swal_Tips("序号"+value.fEntryID+"所对应物料的辅助到货数量应为数字");
			return false;
		}
		if(!(value.fClosed==0||value.fClosed==1)){
			flag = 0;
			swal_Tips("请选择序号"+value.fEntryID+"所对应物料是否关闭");
			return false;
		}
		if(value.fStockID == null || value.fStockID == ""){
			flag = 0;
			swal_Tips("请选择序号"+value.fEntryID+"所对应物料的接收仓库");
			return false;
		}*/
		/*if($('#fWorkerName').val() == null || $('#fWorkerName').val()==""){
			flag = 0;
			swal_Tips("请输入操作人卡号");
			return false;
		}*/
		datass={
			"fDetailID":value.fDetailID,
			"fArrivalDate":dateFormatter(value.fArrivalDate)+":00",
			"fArrivalQty":Math.round(parseFloat(value.fTempQty)*100)/100,
			"fAuxQty":Math.round(parseFloat(value.fTemp1Qty)*100)/100,
			"fIsColsed":value.fClosed,
			"fStockID":value.fStockID,
			"fName":value.fName,
			"fModel":value.fModel,
			"fNumber":value.fNumber,
			"fSecCoefficient":value.fSecCoefficient,
			"fTaxrate":value.fTaxrate,
			"fTaxPrice":value.fTaxPrice,
			"fPrice":Math.round(parseFloat(value.fPrice)*100000000)/100000000,
			"fUnitName":value.fUnitName,
			"fSPID":0,
			"fWorker":$('#fWorker').val(),
			"fOrderQty":value.fQty
		}
		dataJson.push(datass);
		console.log(datass);
	});
	if(flag == 1){
		showLoading();
		console.log( JSON.stringify(dataJson));
		$.ajax({
			url : pathUrl("poOrder/stockArrivalList.action"),
			type : "POST",
			dataType : "JSON",
			data : {
				'arrivalListStr' : JSON.stringify(dataJson),
			},
			success : function(data) {
				if (data.code == "0000") {
					swal_Tips_Short(data.message);
					$("#inputCardModal").modal('hide');
					$('#daoTable').bootstrapTable('refresh');
					$(".clean_C").val("");
					console.log($("#printtype option:selected").val());
					if($("#printtype option:selected").val()=='1'){
						window.open(pathUrl("OSL?ids="+data.rows+"&type=0"));
					}
					else if($("#printtype option:selected").val()=='2'){
						window.open(pathUrl("OSL?ids="+data.rows+"&type=1"));
					}else{
						window.open(pathUrl("OSL?ids="+data.rows+"&type=2"));
					}
					//window.open(pathUrl("OSL?ids="+data.rows+"&type=0"));
				} else if (data.code == "9999") {
					swal_Tips(data.message);
				}
			},
			error : function(request) {
				swal_Tips_Error();
			},
			complete : function () {
			}
		});
		}
})
//复制模态框 确认按钮
$("#copyconfirm").click(function() {
	let flag = 1;
	let dataJson=[];
	let datass={};
	let datas = $("#copyTable").bootstrapTable('getData');//获取所有的row
	console.log(datas);
	let reg1 = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/;
	let reg2 = /^(?:[1-3]?[1-9]\d{0,6}(?:\.\d+)?|0\.\d+)$/
	$.each(datas,function(key,value){
		console.log(parseInt(value.fQty));
		if (!reg2.test(parseInt(value.fQty))){
			    flag = 0;
				swal_Tips("采购数量应是大于0的数");
				return false;
		}
		/*else if(!reg1.test(parseInt(value.fPrice))){
			flag = 0;
			swal_Tips("价格应是大于等于0的数");
			return false;
		}*/
		datass={
			"fInterID":value.fInterID,
			"fItemID":value.fItemID,
			"fQty":parseFloat(value.fQty),
			"fPrice":parseFloat(value.fPrice),
		}
		dataJson.push(datass);
		console.log(datass);
	});
	if(flag == 1){
		showLoading();
		console.log( JSON.stringify(dataJson));
		$.ajax({
			url : pathUrl("poOrder/addPoorderEntry.action"),
			type : "POST",
			dataType : "JSON",
			data : {
				'datas' : JSON.stringify(dataJson),
			},
			success : function(data) {
				if (data.code == "0000") {
					swal_Tips_Short(data.message);
					$("#copyinputCardModal").modal('hide');
					$('#copyTable').bootstrapTable('refresh');
					$(".clean_C").val("");
				} else if (data.code == "9999") {
					swal_Tips(data.message);
				}
			},
			error : function(request) {
				swal_Tips_Error();
			},
			complete : function () {
			}
		});
		}
})
/*
 * 合并单元格函数
 * 
 */
function hebing(){
	var msg = $('#inTable').bootstrapTable('getData');
	if(msg==""){
		return false;
	}
	var bh=msg[0].fBillNo;
	var index = 0;
	var z;
	for(z= 1;z<msg.length;z++){
		var bhx = msg[z].fBillNo;
		if(bhx != bh){
			$('#inTable').bootstrapTable('mergeCells', {index: index, field: 'fBillNo', rowspan: z - index});
			$('#inTable').bootstrapTable('mergeCells', {index: index, field: 'fSupplyName', rowspan: z - index});
			$('#inTable').bootstrapTable('mergeCells', {index: index, field: 'operate', rowspan: z - index});
			bh=bhx;
			index = z;
		}
	}
	if(z == msg.length){
		$('#inTable').bootstrapTable('mergeCells', {index: index, field: 'fBillNo', rowspan: z - index});
		$('#inTable').bootstrapTable('mergeCells', {index: index, field: 'fSupplyName', rowspan: z - index});
		$('#inTable').bootstrapTable('mergeCells', {index: index, field: 'operate', rowspan: z - index});
	}
	$("#inTable tr td").each(function(){// 遍历表里的td
		var index = $(this).attr('rowspan');// 获取是几行合并的
		var $this = $(this).parent();// 获取父节点
		if(index != undefined && index != 1){
			for(var x=1;x<index;x++){// 获取合并行的最后一行
				$this = $this.next();
			}
			$(this).css({'borderBottom':'2px solid'});
			$this.css({'borderBottom':'2px solid'});
		}
		if(index == 1){
			$(this).siblings().css({'borderBottom':'2px solid'});
		}
    });
}
/*var rowIndex = "%^%%%%";//接收表的下标（用于改变选中行样式）
var page;//接收表的页码
var _index = -1;//接收表的下标（用于表勾选）
var _fCode;//派工单号
var fid="",num="";
var dataExport = {//导出传值
	row:10000000//必传不然默认导出来就是10条数据
};
var colArr = ['0','checkStatus','operate'];

 * @Description: 预加载
 * @param : 
 
$(function() {
	resizeIframeHight();
	initOutTable();
    $.ajax({
		url : pathUrl("stock/getStockList.action"),
		type : 'POST',
		dataType : "JSON",
	}).then(function(data) {
		var str='';
		for(key in data){
			str+='<option value="'+data[key].fStockID+'">'+data[key].fStockName+'</option>';
		}
		$("#fStockId").html(str);
		$('.selectpicker').selectpicker('refresh')
	}) 
	//点击Tab刷新表格
	var time = {
		onClose : false,
		format : 'YYYY-MM-DD',
		donefun : function(obj) {
			// end.minDate = obj.val; // 开始日选好后，重置结束日的最小日期
		}
	};
	jeDate('#starttime',time);
	jeDate('#endtime',time);
	$(".CGDH",parent.document).click(function(){
		$('#outTable').bootstrapTable('refresh');
//		setTimeout(function(){
//			if(_index != -1){
//				$('#pickTable').bootstrapTable('check',_index);
//			}
//		},500);
	});
});


/*
 * @Description: 初始化接收列表 
 * @param :  
 
function initOutTable() {
	$('#outTable').bootstrapTable('destroy').bootstrapTable({
		url : pathUrl("poOrder/notArrivalPoOrderList.action"),//请求后台的URL
		//height : $(window).height() - 200,//行高
		method : "POST",//请求方式
		//striped : true,//是否显示行间隔色
		cache : false,//是否使用缓存，默认为true
		pagination : true,//是否显示分页
		clickToSelect : true,//是否启用点击选中行
		singleSelect : true,//是否单选
		pageNumber : 1,//初始化加载第几页
		pageSize : 10,//每页的记录行数
		sidePagination : "server", // 分页方式：client客户端分页，server服务端分页（*）
		pageList : [ 10, 25, 50, 100 ],// 分页方式：client客户端分页，server服务端分页
		colArr : colArr,
		dataExport : dataExport,
		showExport: true,
		exportDataType: "allServer",
		rowStyle : function(row, index) {//自定义样式，参数为：row：行数据，index：行下标
			var classes = [ 'success' ];
			if (index == rowIndex) {
				return {
					classes : classes[0]
				};
			}
			return {};
		},
		queryParams : function(params) {//传递参数
			var data= {};
			data.offset = params.offset;
			data.row = params.limit;
			data.fSupplyName=$("#fSupplyName").val();
			data.fBillNo=$("#fBillNo").val();
			data.fNumber=$("#fNumber").val();
			data.fName=$("#fName").val();
			data.fModel=$("#fModel").val();
			data.startTime =  $("#starttime").val();
			data.endTime =  $("#endtime").val();
			return data;
		},
		dataType : 'JSON',//数据类型
		uniqueId : "id",//每一行的唯一标识，一般为主键列
		columns : [ {
			checkbox : true
		}, {
			field : "fBillNo",
			align : 'left',
			valign: 'middle',
			title : '采购单号',
			edit : false
		}, {
			field : "fSupplyName",
			align : 'left',
			valign: 'middle',
			title : '供应商',
			edit : false
		}, {
			field : "fEntryID",
			align : 'left',
			valign: 'middle',
			title : '序号',
			edit : false
		}, {
			field : "fNumber",
			align : 'left',
			valign: 'middle',
			title : '物料代码',
			edit : false
		},{
			field : "fName",
			align : 'left',
			valign: 'middle',
			title : '物料名称',
			edit : false
		},{
			field : "fModel",
			align : 'left',
			valign: 'middle',
			title : '规格型号',
			edit : false
		},{
			field : "fQty",
			align : 'left',
			valign: 'middle',
			title : '采购数量',
			edit : false
		}, {
			field : "fCommitQty",
			align : 'left',
			valign: 'middle',
			title : '到货数量',
			edit : false	
		}, {
			field : "fDate",
			align : 'left',
			valign: 'middle',
			title : '下单日期',
			edit : false,
			formatter:dateFormatter
		}, {
			field : "fArrivalDate",
			align : 'left',
			valign: 'middle',
			title : '预计到货日期',
			edit : false,
			formatter:dateFormatter
		}, {
			field : "fStockQty",
			align : 'left',
			valign: 'middle',
			title : '入库数量',
			edit : false
		}, {
			field : 'operate',//如需导出execl field必填operate，否则操作行会被导出
			align : 'left',
			valign: 'middle',
			title : '操作',
			formatter : function(value,row,index){
				if(row.fQcStatus==0||row.fQcStatus==null){
					return '<button type="button" class="btn btn-default in" style="font-size:12px;margin-right:5px;">到货</button>';
				}else{
					return '';
				}
			},
			events : operateEvents,
			edit : false
		}],
		onClickRow : function(row, $element) {//行单击
//			console.log(row)
			$('.success').removeClass('success');// 去除之前选中的行的，选中样式
			$($element).addClass('success');// 添加当前选中的 success样式用于区别
			index = $element.data('index');
			rowIndex = index;
			page = $('.pagination .active').find("a").eq(0).text();
		},
		onPageChange : function(number, size) {//翻页
			if (page == number) {
				rowIndex = index;
			} else {
				$('.success').removeClass('success');
				rowIndex = "%^%%%%";
			}
		},
		onUncheck : function(index){//取消勾选
			$('.success').removeClass('success');
			rowIndex = "%^%%%%";
		},
		exportHandler : function(text){
			if(text == 'success'){
				$('#export').text('导出成功');
				setTimeout(function(){
					$('#export').text('导出');
					$('#export').blur();
					$('#export').removeAttr('disabled');
				},3000)
			}
		}
	});
}

 * @Description: 表按钮操作事件
 * @param :
 
window.operateEvents = {
		'click .in' : function(e, value, row, index) {//到货按钮点击事件
		    $("#fArrivalQty").val("");
		    $("#fWorker").val("");
		    $("#fWorkername").val("");
		    $("#fOIHighLimit").val(row.fOIHighLimit)
			$("#fno").val(row.fBillNo);
		    $("#fDetailID").val(row.fDetailID)
		    $("#infSupplierName").val(row.fSupplyName)
		    $("#fCommitQty").val(row.fCommitQty);
		    $("#infNumber").val(row.fNumber)
		    $("#infName").val(row.fName)
		    $("#infModel").val(row.fModel)
		    $("#fQty").val(row.fQty)
		    $('.selectpicker').selectpicker('val', row.fStockID);
		    $('.selectpicker').selectpicker('refresh');
		    $("#infTranOutQty").val(dateFormatter(row.fDate));
		    $("#fArrivalDate").val(dateFormatter(row.fArrivalDate));
		    $('#sw-content input').bootstrapSwitch('state',false);
			$("#inputCardModal").modal('show');
		}
	}

 * @Description: 到货事件
 * @param :
 
var fIsColsed= 0;
$('#sw-content input').bootstrapSwitch();
$('#sw-content input').bootstrapSwitch('state',false);
$('#sw-content input').on('switchChange.bootstrapSwitch', function (event,state) {
	if(state){
		fIsColsed = 1;
	}else{
		fIsColsed = 0;
	}
});
$("#confirm").click(function() {
	var num=(1+$("#fOIHighLimit").val()/100)*$("#fQty").val()-$("#fCommitQty").val();
//	var num=$("#fOIHighLimit").val()*$("#fQty").val()-$("#fCommitQty").val();
	if($("#fArrivalQty").val()>num){
		swal_Tips("本次入库数量不能超过最大上限!");
		return false;
	}
	showLoading();
	$.ajax({
		type : "POST",
		url : pathUrl("poOrder/stockArrival.action"),
		dataType : "JSON",
		data : {
			fDetailID:$("#fDetailID").val(),
			fIsColsed:fIsColsed,
			fArrivalQty:$("#fArrivalQty").val(),
			fStockId:$("#fStockId").val(),
			fSPID:0,
			ArrivalDate:new Date($("#fArrivalDate").val()),
			fWorker:$("#fWorker").val()
		},
		success : function(data) {
//			console.log(data);
			if(data.code=="9999"){
				swal_Tips(data.message);
			} else if(data.code="0000") {
				swal_Tips_Short(data.message);
				$("#inputCardModal").modal('hide');
				$('#outTable').bootstrapTable('refresh');
				window.open(pathUrl("OSL?id="+ data.id+"&type=0"));
			}
		},
		error : function(request) {
			swal_Tips_Error();
		}
	});
})

function searchyg() {
	$.ajax({
		url : pathUrl("emp/findByCondition.action"),
		type : 'GET',
		dataType : "JSON",
		data : {
			"row" : 100000,
			"fNumber" :  $("#fWorker").val()
		},
	}).then(function(data) {
//		console.log(data)
		var str = "";
		if(data.rows != null && data.rows.length == 1){//查询到数据并且只有一条数据，才写入员工姓名
			str = data.rows[0].fName;
		}
		$("#fWorkername").val(str);
	})
}
 @Description: 查询按钮单击事件
*  @param :

$("#query").click(function() {
	initOutTable();
});

* @Description: 重置按钮单击事件
* @param :

$("#resetting").click(function() {
	$("#fBillNo").val("");
	$("#fSupplyName").val("");
	$("#fNumber").val("");
	$("#fName").val("");
	$("#fModel").val("");
	$("#starttime").val("");
	$("#endtime").val("");
	initOutTable("");
});

 * @Description: 查询条件按回车事件
 * @param :
 
$("#fSupplyName,#fBillNo,#fNumber,#fName,#fModel").keypress(function(event){
	if(event.keyCode == "13"){
		initOutTable();
	}
});

 * @Description: 导出按钮点击事件
 * @param :
 
$("#exporting").click(function(){
	$('#outTable').bootstrapTable('refreshOptions',{colArr,colArr});
	$('#exportTableBtn').eq(0).trigger('click');
});*/